/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#include <billitem.h>

QString RecurringPeriodType::recurringPeriodTypeToString(
    RecurringPeriodType::RecurringPeriodTypes recurringPeriodType)
{
    QString recurringPeriodTypeString = "";

    switch (recurringPeriodType) {
        case RecurringPeriodType::DAILY:
            recurringPeriodTypeString = "Daily";
            break;
        case RecurringPeriodType::WEEKLY:
            recurringPeriodTypeString = "Weekly";
            break;
        case RecurringPeriodType::MONTHLY:
            recurringPeriodTypeString = "Monthly";
            break;
        case RecurringPeriodType::LASTDAYOFMONTH:
            recurringPeriodTypeString = "Last day of the month";
            break;
        case RecurringPeriodType::YEARLY:
            recurringPeriodTypeString = "Yearly";
            break;
        case RecurringPeriodType::NONE:
        default:
            recurringPeriodTypeString = "None";
            break;
    }

    return recurringPeriodTypeString;
}

RecurringPeriodType::RecurringPeriodTypes
    RecurringPeriodType::stringToRecurringPeriodType(QString type)
{
    RecurringPeriodType::RecurringPeriodTypes recurringPeriodType;

    if (type.simplified().toLower() == "daily") {
        recurringPeriodType = RecurringPeriodType::DAILY;
    }
    else if (type.simplified().toLower() == "weekly") {
        recurringPeriodType = RecurringPeriodType::WEEKLY;
    }
    else if (type.simplified().toLower() == "monthly") {
        recurringPeriodType = RecurringPeriodType::MONTHLY;
    }
    else if (type.simplified().toLower() == "last day of the month") {
        recurringPeriodType = RecurringPeriodType::LASTDAYOFMONTH;
    }
    else if (type.simplified().toLower() == "yearly") {
        recurringPeriodType = RecurringPeriodType::YEARLY;
    }
    else {
        recurringPeriodType = RecurringPeriodType::NONE;
    }

    return recurringPeriodType;
}

BillItemData::BillItemData()
{
    m_amountDue = 0;
    m_autoPay = false;
    m_dateDue = 0;
    m_id = 0;
    m_payee = 0;
    m_paymentLocation = 0;
    m_paymentMethod = 0;
    m_paymentRecurringPeriod = RecurringPeriodType::NONE;
    m_paid = false;
    m_name = 0;
    m_notes = 0;
    m_tags = 0;
}

BillItemData::BillItemData(const BillItemData &other) : QSharedData(other)
{
    if (other.m_amountDue) {
        m_amountDue = new QString(*other.m_amountDue);
    }
    else {
        m_amountDue = 0;
    }

    m_autoPay = other.m_autoPay;

    if (other.m_dateDue) {
        m_dateDue = new QDate(*other.m_dateDue);
    }
    else {
        m_dateDue = 0;
    }

    if (other.m_id) {
        m_id = new QString(*other.m_id);
    }
    else {
        m_id = 0;
    }

    if (other.m_payee) {
        m_payee = new QString(*other.m_payee);
    }
    else {
        m_payee = 0;
    }

    if (other.m_paymentLocation) {
        m_paymentLocation = new QString(*other.m_paymentLocation);
    }
    else {
        m_paymentLocation = 0;
    }

    if (other.m_paymentMethod) {
        m_paymentMethod = new QString(*other.m_paymentMethod);
    }
    else {
        m_paymentMethod = 0;
    }

    m_paymentRecurringPeriod = RecurringPeriodType::NONE;

    m_paid = other.m_paid;

    if (other.m_name) {
        m_name = new QString(*other.m_name);
    }
    else {
        m_name = 0;
    }

    if (other.m_notes) {
        m_notes = new QString(*other.m_notes);
    }
    else {
        m_notes = 0;
    }

    if (other.m_tags) {
        m_tags = new QSet<QString>(*other.m_tags);
    }
    else {
        m_tags = 0;
    }
}

BillItemData::~BillItemData()
{
    delete m_amountDue;
    delete m_dateDue;
    delete m_id;
    delete m_payee;
    delete m_paymentLocation;
    delete m_paymentMethod;
    delete m_name;
    delete m_notes;
    delete m_tags;
}

BillItem::BillItem()
{
    d = new BillItemData;
}

QString BillItem::getAmountDue() const
{
    if (!d->m_amountDue) {
        return QString();
    }
    return *d->m_amountDue;
}

bool BillItem::getAutoPay() const
{
    return d->m_autoPay;
}

QDate BillItem::getDateDue() const
{
    if (!d->m_dateDue) {
        return QDate();
    }
    return *d->m_dateDue;
}

QString BillItem::getId() const
{
    if (!d->m_id) {
        return QString();
    }
    return *d->m_id;
}

QString BillItem::getPayee() const
{
    if (!d->m_payee) {
        return QString();
    }
    return *d->m_payee;
}

QString BillItem::getPaymentLocation() const
{
    if (!d->m_paymentLocation) {
        return QString();
    }
    return *d->m_paymentLocation;
}

QString BillItem::getPaymentMethod() const
{
    if (!d->m_paymentMethod) {
        return QString();
    }
    return *d->m_paymentMethod;
}

RecurringPeriodType::RecurringPeriodTypes BillItem::getPaymentRecurringPeriod()
    const
{
    return d->m_paymentRecurringPeriod;
}

bool BillItem::getPaid() const
{
    return d->m_paid;
}

QString BillItem::getName() const
{
    if (!d->m_name) {
        return QString();
    }
    return *d->m_name;
}

QString BillItem::getNotes() const
{
    if (!d->m_notes) {
        return QString();
    }
    return *d->m_notes;
}

QSet<QString> BillItem::getTags() const
{
    if (!d->m_tags) {
        return QSet<QString>();
    }
    return *d->m_tags;
}

void BillItem::setAmountDue(const QString &amountDue)
{
    if (!d->m_amountDue) {
        d->m_amountDue = new QString();
    }
    *d->m_amountDue = amountDue.trimmed();
}

void BillItem::setAutoPay(bool autoPay)
{
    d->m_autoPay = autoPay;
}

void BillItem::setDateDue(const QDate &dateDue)
{
    if (!d->m_dateDue) {
        d->m_dateDue = new QDate();
    }
    *d->m_dateDue = dateDue;
}

void BillItem::setId(const QString &id)
{
    if (!d->m_id) {
        d->m_id = new QString();
    }
    *d->m_id = id.trimmed();
}

void BillItem::setPayee(const QString &payee)
{
    if (!d->m_payee) {
        d->m_payee = new QString();
    }
    *d->m_payee = payee.trimmed();
}

void BillItem::setPaymentLocation(const QString &paymentLocation)
{
    if (!d->m_paymentLocation) {
        d->m_paymentLocation = new QString();
    }
    *d->m_paymentLocation = paymentLocation.trimmed();
}

void BillItem::setPaymentMethod(const QString &paymentMethod)
{
    if (!d->m_paymentMethod) {
        d->m_paymentMethod = new QString();
    }
    *d->m_paymentMethod = paymentMethod.trimmed();
}

void BillItem::setPaymentRecurringPeriod(
    RecurringPeriodType::RecurringPeriodTypes paymentRecurringPeriod)
{
    d->m_paymentRecurringPeriod = paymentRecurringPeriod;
}

void BillItem::setPaid(bool paid)
{
    d->m_paid = paid;
}

void BillItem::setName(const QString &name)
{
    if (!d->m_name) {
        d->m_name = new QString();
    }
    *d->m_name = name.trimmed();
}

void BillItem::setNotes(const QString &notes)
{
    if (!d->m_notes) {
        d->m_notes = new QString();
    }
    *d->m_notes = notes.trimmed();
}

void BillItem::setTags(const QSet<QString> &tags)
{
    if (!d->m_tags) {
        d->m_tags = new QSet<QString>();
    }
    *d->m_tags = tags;
}

void BillItem::addTag(const QString &tag)
{
    if (!d->m_tags) {
        d->m_tags = new QSet<QString>();
    }
    d->m_tags->insert(tag);
}

void BillItem::removeTag(const QString &tag)
{
    if (!d->m_tags) {
        d->m_tags = new QSet<QString>();
    }
    d->m_tags->remove(tag);
}

QString BillItem::stringSetToString(QSet<QString> stringSet)
{
    QString strings = "";
    Q_FOREACH(QString string, stringSet)
    {
        strings += string + " ";
    }

    return strings.simplified();
}
